package bb.lanxing.activity.routebook;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.activity.sport.SportActivity;
import bb.lanxing.adapter.RouteBookNormalAdapter;
import bb.lanxing.model.database.RouteBook;

public class RouteBookChooseActivity extends BaseActivity {
    public static final String INTENT_TYPE = "is_need_choose";
    private RouteBookNormalAdapter adapter;
    private ListView listView;
    private List<RouteBook> routeBookList = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_lushu_choose);
        bindView(getWindow().getDecorView());
        setupActionBar(true);
        setTitle(R.string.lushu_choose_title);
        boolean isNeedChoose = getIntent().getBooleanExtra(INTENT_TYPE, false);
        long longExtra = getIntent().getLongExtra(SportActivity.EXTRA_LUSHU_ID, -1L);
        adapter = new RouteBookNormalAdapter(this, routeBookList);
        adapter.setIsNeedChoose(isNeedChoose);
        adapter.setChooseId(longExtra);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((adapterView, view, i, j) -> {
            Intent intent = new Intent(RouteBookChooseActivity.this, RouteBookDetailMapActivity.class);
            intent.putExtra(SportActivity.EXTRA_LUSHU_ID, (routeBookList.get((int) j)).getId());
            startActivity(intent);
        });
        loadData();
    }

    private void loadData() {
        List<RouteBook> shareLushuses = RouteBook.getList();
        routeBookList.clear();
        routeBookList.addAll(shareLushuses);
        adapter.notifyDataSetChanged();
    }

    private void bindView(View view) {
        listView = (ListView) view.findViewById(R.id.listView);
    }
}
